package com.flex.mobile.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;

import com.flex.mobile.dao.IGenericDAO;
import com.flex.mobile.dao.model.DriverOrder;

@Service("driverOrderDao")
public class DriverOrderDao implements IGenericDAO<DriverOrder> {
	private final SimpleJdbcTemplate template;
	private final SimpleJdbcInsert insertContact;
	private final SimpleJdbcInsert insertTempContact;

	private final RowMapper<DriverOrder> summaryRowMapper = new ParameterizedRowMapper<DriverOrder>() {

		public DriverOrder mapRow(ResultSet rs, int rowNum) throws SQLException {
			DriverOrder model = new DriverOrder();
			model.setId(rs.getLong("ID"));
			model.setDriverId(rs.getLong("DRIVER_ID"));
			model.setOrderId(rs.getLong("ORDER_ID"));
			model.setCreateDate(rs.getDate("CREATE_DATE"));
			return model;
		}
	};

	@Autowired
	public DriverOrderDao(DataSource dataSource) {
		this.template = new SimpleJdbcTemplate(dataSource);
		this.insertContact = new SimpleJdbcInsert(dataSource).withTableName("R_DRIVER_ORDER").usingGeneratedKeyColumns("ID");
		this.insertTempContact = new SimpleJdbcInsert(dataSource).withTableName("T_DRIVER_ORDER").usingGeneratedKeyColumns("ID");
	}

	@Override
	public List<DriverOrder> findAll() {
		return this.template.query("SELECT * FROM R_DRIVER_ORDER", this.summaryRowMapper);
	}

	public long countAll() {
		return this.template.queryForInt("select count(1) from driver_info ");
	}

	/**
	 * 查司机订单临时关系表
	 * 
	 * @return
	 */
	public List<DriverOrder> findTempAll() {
		return this.template.query("SELECT * FROM T_DRIVER_ORDER", this.summaryRowMapper);
	}

	@Override
	public List<DriverOrder> findByName(String name) {

		return null;
	}

	@Override
	public DriverOrder findById(long id) {
		return this.template.queryForObject("select * from R_DRIVER_ORDER where id=?", this.summaryRowMapper, id);
	}

	public DriverOrder findTempById(long id) {
		return this.template.queryForObject("select * from T_DRIVER_ORDER where id=?", this.summaryRowMapper, id);
	}

	public List<DriverOrder> findByDriverId(long driverId) {
		return this.template.query("select * from R_DRIVER_ORDER where DRIVER_ID=?", this.summaryRowMapper, driverId);
	}

	public List<DriverOrder> findByOrderId(long orderId) {
		return this.template.query("select * from R_DRIVER_ORDER where ORDER_ID=?", this.summaryRowMapper, orderId);
	}

	public List<DriverOrder> findTempByDriverId(long driverId) {
		return this.template.query("select * from T_DRIVER_ORDER where DRIVER_ID=?", this.summaryRowMapper, driverId);
	}

	public List<DriverOrder> findTempByOrderId(long orderId) {
		return this.template.query("select * from T_DRIVER_ORDER where ORDER_ID=?", this.summaryRowMapper, orderId);
	}

	@Override
	public DriverOrder create(DriverOrder driverOrder) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("ID", null);
		parameters.put("DRIVER_ID", driverOrder.getDriverId());
		parameters.put("ORDER_ID", driverOrder.getOrderId());
		Number id = this.insertContact.executeAndReturnKey(parameters);
		driverOrder.setId(id.intValue());
		return driverOrder;
	}

	public DriverOrder createTemp(DriverOrder driverOrder) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("ID", null);
		parameters.put("DRIVER_ID", driverOrder.getDriverId());
		parameters.put("ORDER_ID", driverOrder.getOrderId());
		Number id = this.insertTempContact.executeAndReturnKey(parameters);
		driverOrder.setId(id.intValue());
		return driverOrder;
	}

	@Override
	public boolean update(DriverOrder item) {
		return false;
	}

	@Override
	public boolean remove(DriverOrder item) {
		return false;
	}

}
